package com.sunbo509.www.gridview;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceView;
import android.view.View;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Media2Activity extends AppCompatActivity {
         private SurfaceView sv;
         private ArrayList<String>list=new ArrayList<>();
//         private MediaPlayer player=null;
         private static MediaPlayer media;
    private int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media2);
        sv=(SurfaceView)findViewById(R.id.sv1);



        for(int i=0;i<3;i++){
            //真机路径
            String data =getSDPath() + "/"+"DCIM"+"/"+"Camera"+"/" + "do.mp4";
            String data1=getSDPath() + "/"+"DCIM"+"/"+"Camera"+"/" + "do1.mp4";
            String data2=getSDPath() + "/"+"DCIM"+"/"+"Camera"+"/" + "do2.mp4";
            //虚拟机路径
//            String data =getSDPath() + "/" + "do.mp4";
//            String data1=getSDPath() + "/" + "do1.mp4";
//            String data2=getSDPath() + "/" + "do2.mp4";
            if(i==0)
                list.add(data);
            else if(i==1)
                list.add(data1);
            else if(i==2)
                list.add(data2);
        }
        if(list.size()>0){
            sv.setVisibility(View.VISIBLE);
            media=getMedia();
            media.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
              start();
            } catch (IOException e) {
                e.printStackTrace();
            }

            media.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.reset();
                    index++;
                    if(index>=list.size())
                        index=0;
                    try {
                        start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });




        }

    }

public void start() throws IOException {
    media.setDataSource(list.get(index));
    media.prepareAsync();
    media.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setDisplay(sv.getHolder());
            mp.start();
        }
    });
}
    //获取SD目录
    public String getSDPath(){
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if(sdCardExist)
        {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
       // return sdDir.toString();
        return Environment.getExternalStorageDirectory().toString();
    }


    public MediaPlayer getMedia(){
        if(media==null){
            media=new MediaPlayer();
        }
        return media;
    }

}
